package logic

import (
	"context"
	"errors"
	"srv/models"

	"srv/house"
	"srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseCreateLogic {
	return &HouseCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseCreateLogic) HouseCreate(in *house.HouseCreateRequest) (*house.HouseCreateResponse, error) {
	// todo: add your logic here and delete this line
	var newUser models.User
	err := newUser.FirstUser(int(in.UserId))
	if err != nil {
		logx.Error("用户未登录,不允许发布")
		return nil, errors.New("用户未登录,不允许发布")
	}
	if newUser.Status != 2 {
		logx.Error("用户信息审核未通过,不允许发布")
		return nil, errors.New("用户信息审核未通过,不允许发布")
	}
	var newHouse models.House
	err = newHouse.FindHouse(in.HouseName)
	if err == nil {
		logx.Error("房源已存在")
		return nil, errors.New("房源已存在")
	}
	m := models.House{
		UserId:        int(in.UserId),
		HouseName:     in.HouseName,
		HouseCity:     in.HouseCity,
		HouseAdd:      in.HouseAdd,
		HouseBuilding: in.HouseBuilding,
		HouseFloor:    int(in.HouseFloor),
		HouseArea:     in.HouseArea,
		HousePrice:    float64(in.HousePrice),
		HouseInfo:     in.HouseInfo,
		Status:        0,
	}
	err = m.CreateHouse()
	if err != nil {
		logx.Error("房源发布失败")
		return nil, errors.New("房源发布失败")
	}
	return &house.HouseCreateResponse{
		HouseId: int64(m.ID),
	}, nil
}
